package com.miku.framework.ui.view;

import com.miku.R;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class Menu extends View{
	
	private Intent intent;
	private Bitmap image;
	private int width;
	private int length;

	public Menu(Context context) {
		super(context);
		// TODO Falta implementar para cuando no se ingresa atributos por xml layout
	}
	
	public Menu(Context context, AttributeSet attrs){
		super(context, attrs);
		
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Menu);
		
		final int numAttributes = a.getIndexCount();
		
		for (int i=0; i<numAttributes; i++){
			switch (a.getIndex(i)){
				case R.styleable.Menu_image:
					int resourceId = a.getResourceId(i, -1);
					if (resourceId > -1)
						image = BitmapFactory.decodeResource(getResources(), resourceId);
					else{
						Log.e("Menu.java", "Error, no se encontró imagen");
						throw new RuntimeException("No se ha especificado el recurso correcto de imagen");
					}
				break;
				case R.styleable.Menu_intent:
					String action = a.getString(i);
					intent = new Intent(action);
				break;
				case R.styleable.Menu_length:
					length = a.getInteger(i, 30);
				break;
				case R.styleable.Menu_width:
					width = a.getInt(i, 30);
				break;
			}
		}
		a.recycle();
	}
	
	@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, length);
    }
	
	@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        canvas.drawBitmap(image, null, new Rect(0,0,width,length), null);
    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		this.getContext().startActivity(intent);
		
		return super.onTouchEvent(event);
		
	}
	
	
	
}
